/**
 * CommonProperty
 */

  var ReactDOM = require("react-dom");
  var tool = require("../../../utils/tool");
  var differUpdateHelper = require("../../../utils/DifferUpdateHelper");
  var CommonProperty = {
      componentWillMount: function (){
          this.initialize = false;
      },
      componentDidMount: function (){
          if (!this.initialize){
              this.initialize = true;

              this.bindKeydown();
              this.bindChangeEvent();
          }
          this.keyCodes = {
              enter: 13
          };
      },
      bindKeydown: function(){
          var dom = $(ReactDOM.findDOMNode(this));
          if (dom && dom.length){
              var element = dom.find("input");
              if (element && element.length){
                  var me = this;
                  element.on("keydown", function (e){
                      element = $(this);
                      var code = e.keyCode;
                      if (code == me.keyCodes.enter){
                          if (typeof me.handleOnInputBlur === "function"){
                              var type = element.attr("name");
                              if (type){
                                  element.blur();
                              }
                              e.stopPropagation();
                              e.preventDefault();
                              return false;
                          }
                      }
                  });
              }
          }
      },
      bindChangeEvent: function (){
          var dom = $(".property-container");
          if (dom && dom.length){
              var me = this;
              dom.on("keyup change", "input,select,textarea", function (){
                  var target = $(this), name = target.attr("name"), type = target.attr("type"), value = target.val();
                  var data = me.props.widget;
                  if (name && type){
                      // 输入为空时获取默认值
                      value = (value === "" ? (type === "number" ? 0 : "") : value);

                      var param = {};
                      param[name] = value;

                      data = $.extend(data, param);

                      // 更新差异化列表
                      differUpdateHelper.batch(data);
                  }
              });
          }
      }
  };

  module.exports = CommonProperty;
